﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;

//本页对所有日志进行查询，并可直接查看详情
public partial class System_LogViewer : PageBase
{
   
    static decimal  count = -1,start_record = 0, end_record = 10, pages = 0, pagesize = 15,curpage=1;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
         
            FillDllLogTypeKey();


            Search();
        }
    }
    #region 数据绑定

    protected void PageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        gridViewLogInfo.PageIndex = e.NewPageIndex;
        Search();
    }
    /// <summary>
    /// 绑定数据
    /// </summary>
    protected void Search()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        EventManager eventManager = new EventManager();
       
        
       
        string logtype = ddlLogType.SelectedValue;
        if (logtype != "ALL")
        {
            sb.Append(" AND t1.LogTypeKey='" + logtype + "' ");
        }
        if (txtStateDate.Text.Trim() != "")
            sb.Append(" AND t1.LogCreateDate >'" + txtStateDate.Text.Trim()+"' ");
        if (txtEndDate.Text.Trim() != "")
            sb.Append(" AND t1.LogCreateDate<'" + txtEndDate.Text.Trim()+"' ");

        if (txtLoginName.Text != "")
            sb.Append(" AND t1.LogUserName like '%"+txtLoginName.Text.Trim()+"%' ");
       
        count = eventManager.GetCount(sb.ToString());
        ViewState["sb"] = sb.ToString();
        if (count > 0)
        {
            pages = Decimal.Floor(count / pagesize);//总页数
            pages = (count / pagesize) > pages ? (pages + 1) : pages;
            start_record = 1;
            end_record = pagesize;
            curpage = 1;
            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
            BindFrom(start_record, end_record);
        }
        else
        {
            start_record = 0;
            end_record = 0;
            pages = 0;
            curpage = 0;
            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", 0, 0, 0);
            this.gridViewLogInfo.DataSource = null;
            this.gridViewLogInfo.DataBind();
           // BindFrom(0, 0);
        }

       
        //利用存储过程的特殊查询方法
       // this.gridViewLogInfo.DataSource = eventManager.SelectInfoByPage(txtLoginName.Text.Trim().Replace("'", ""),
                                 //  logtype, startdate, enddate, this.gridViewLogInfo.PageSize, this.gridViewLogInfo.PageIndex, out tmp);
       // this.gridViewLogInfo.DataBind();
       
    }

    private void BindFrom(decimal start, decimal end)
    {
        EventManager eventManager = new EventManager();
        string sb = "";
        if (ViewState["sb"] != null)
            sb = ViewState["sb"].ToString();
        this.gridViewLogInfo.DataSource = eventManager.GetByPage(start_record, end_record, sb);
        this.gridViewLogInfo.DataBind();
    }
    
    /// <summary>
    /// 填充日志类型
    /// </summary>
    private void FillDllLogTypeKey()
    {
        this.ddlLogType.Items.Clear();
        this.ddlLogType.Items.Add("ALL");
        EventLogTypesManager eltm = new EventLogTypesManager();
        ObjectTable ot = eltm.GetAll();
        foreach(ObjectRow or in ot)
        {
            EventLogTypes elt = (EventLogTypes)or;
            this.ddlLogType.Items.Add(new ListItem(elt.LogTypeFriendlyName, elt.LogTypeKey));
        }
        this.ddlLogType.SelectedIndex = 0;
    }
  
    //绑定GRIDVIEW中的数据
    protected void gridViewLogInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //DataRowView drv = (DataRowView)e.Row.DataItem;
            //Button btnView = (Button)e.Row.FindControl("btnView");

            //btnView.CommandName = "View";
            //btnView.CommandArgument = drv["LogGUID"].ToString();

        }
    }
    #endregion

    #region 按钮方法
   
    /// <summary>
    /// 选择页码
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
       
    }
    //查询按钮
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Search();
    }

    public string getUrl(string id)
    {
        return "openWindow('LogDetail.aspx?id=" + id + "', 'ChangeRoute','location=no,width=510,height=340,top=0,left=0') ";
        

    }
    #endregion 

    protected void btn_first_Click(object sender, EventArgs e)
    {

        if (curpage != 1&&count>0)
        {
            start_record = 1;
            end_record = pagesize;
            curpage = 1;
            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
            BindFrom(start_record, end_record);
        }


    }
    protected void btn_per_Click(object sender, EventArgs e)
    {
        if (curpage>1)
        {
            end_record = start_record-1;
            start_record = start_record -pagesize;
            
            curpage--;
            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
            BindFrom(start_record, end_record);
        }
    }
    protected void btn_next_Click(object sender, EventArgs e)
    {
        if (curpage < pages)
        {
            start_record = end_record+1;
            end_record = end_record + pagesize;
            curpage++;
            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
            BindFrom(start_record, end_record);
        }
    }
    protected void btn_end_Click(object sender, EventArgs e)
    {
        if (curpage <pages)
        {
            start_record = 1;
            end_record = pagesize;
            curpage = 1;

            for (int i = 1; i < pages; i++)
            {
                start_record = end_record + 1;
                end_record = end_record + pagesize;
                curpage++;
            }

            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
            BindFrom(start_record, end_record);

        }
    }
    protected void btnGoto_Click(object sender, EventArgs e)
    {
        int page = 0;
        try
        {
            page = int.Parse(txtPage.Text.Trim());
        }
        catch { page = 0; }
        if (page > 0 & page <= pages)
        {
            start_record = 1;
            end_record = pagesize;
            curpage = 1;

            for (int i = 1; i < page; i++)
            {
                start_record = end_record + 1;
                end_record = end_record + pagesize;
                curpage++;
            }
          
            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
            BindFrom(start_record, end_record);
            
        }
    }
}
